package Servlet;

import Bean.Page;
import Bean.User;
import Service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-08
 * Time: 13:34
 */
//查找用户 -- 按照姓名
@WebServlet("/UserListByNameServlet")
public class UserListByNameServlet extends HttpServlet {
    private UserService userService = new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        String UserName = req.getParameter("UserName");
        //获取当前页数
        String pagination = req.getParameter("pageNo");
        int i = Integer.parseInt(pagination);
        //
        int size = 3;
        Page page = userService.selectByName(i,size, UserName);

        if(page==null) {
            req.setAttribute("status", "error");
            req.setAttribute("message", "数据集不存在,即将返回列表页");
            req.setAttribute("redirectUrl", "UserList?pageNo=1");
            req.setAttribute("waitSeconds", 2);
            req.getRequestDispatcher("skip.jsp").forward(req,resp);
        } else {
            req.setAttribute("list",page.getList());
            req.setAttribute("box",page);
            req.setAttribute("UserName",UserName);
            req.getRequestDispatcher("userByName_list.jsp").forward(req,resp);
        }

    }
}
